package com.mask.im.business.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mask.im.business.model.entity.RechargeNumber;
import com.mask.im.business.model.entity.UserPurse;
import com.mask.im.business.mapper.RechargeNumberMapper;
import com.mask.im.business.mapper.UserPurseMapper;
import com.mask.im.business.common.model.RestResponse;
import com.mask.im.business.service.ChannelService;
import com.mask.im.business.service.RechargeNumberService;
import com.mask.im.business.common.util.MaskIMSecurityUtils;
import com.mask.im.business.common.model.IMSendMessage;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Objects;

import static com.mask.im.business.common.constant.CommonConstant.ZERO;


@Service
@Slf4j
public class RechargeNumberServiceImpl extends ServiceImpl<RechargeNumberMapper, RechargeNumber> implements RechargeNumberService {

	@Autowired
	private UserPurseMapper userPurseMapper;

	@Autowired
	private ChannelService channelService;

	@Override
	public RestResponse<String> createRechargeNumber() {
		Integer userId = MaskIMSecurityUtils.getUserId();
			RechargeNumber initRechargeNumber = RechargeNumber
					.builder()
					.number(String.valueOf(IdWorker.getId()))
					.userId(userId)
					.status(ZERO)
					.build();
			this.save(initRechargeNumber);
			return RestResponse.ok(initRechargeNumber.getNumber());
	}

	@Transactional(rollbackFor = Exception.class)
	public void updateStatus(String number,Integer status,String amount){
		RechargeNumber rechargeNumber = this.baseMapper.selectOne(new LambdaQueryWrapper<RechargeNumber>()
				.eq(RechargeNumber::getNumber, number));
		Integer userId = rechargeNumber.getUserId();
		UserPurse userPurse = userPurseMapper.selectOne(new LambdaQueryWrapper<UserPurse>()
				.eq(UserPurse::getUserId, userId));

		userPurseMapper.update(null,new LambdaUpdateWrapper<UserPurse>()
				.set(UserPurse::getAmount,userPurse.getAmount().add(new BigDecimal(amount)))
				.eq(UserPurse::getUserId,userPurse.getUserId())
				.eq(UserPurse::getVersion,userPurse.getVersion()));

		this.baseMapper.update(null,new LambdaUpdateWrapper<RechargeNumber>()
				.eq(RechargeNumber::getNumber,number)
				.set(RechargeNumber::getStatus,status));

		Channel userIdChannel = channelService.getUserIdChannel(userPurse.getUserId().toString());
		log.info("--------------------支付成功通知---------channel=={},---------->用户id=={}",userIdChannel,userPurse.getUserId().toString());
		if (Objects.nonNull(userIdChannel)){
			System.out.println("--------------------支付成功通知--------------------------");
			log.info("--------------------支付成功通知--------------------------");
			IMSendMessage websocketDTO = new IMSendMessage();
			websocketDTO.setCmd(90001);
			websocketDTO.setData(userPurse.getAmount().add(new BigDecimal(amount)));
			userIdChannel.writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(websocketDTO)));
		}
	}
}

